<?php
/**
 * 约课表
*/

class Book_model extends CT_Model{

    public $_table = 'book';
    public $_table_member = 'member';
    public $_table_teacher = 'teacher';

    public function __construct()
    {
        parent::__construct();
        $this->load->database();
    }
    
    /**
     * 获取汇总列表
     */
    public function summary_book_list($conditions = NULL, $fields = '*', $order = NULL, $start = 0, $limit = NULL){
        try {
            if ($fields != NULL) {
                $this->db->select ( $fields );
            }
            $this->db->from($this->_table);
    
            $this->db->join('book_member','book_member.id = book.book_member_id','join');		//连接用户表
            //$this->db->join('course_package','course_package.id = order.course_package_id','join');		//连接课包表
            $this->db->join('nursery','nursery.id = book.nursery_id','join');		//连接园所
    
            if ($conditions != NULL && $conditions) {
                if (is_array ( $conditions )) {
                    $this->db->where ( $conditions );
                } else {
                    $this->db->where ( $conditions, NULL, FALSE );
                }
            }
            if ($order != NULL) {
                $this->db->order_by ( $order );
            }
            if ($limit != NULL) {
                $this->db->limit ( $limit, $start );
            }
            return $this->db->get()->result_array();
        }catch (Exception $e){
    
        }
    }

    /**
     * 用户提前一小时通知上课
     * @return array
     */
    public function book_before()
    {
        $this->db->select("{$this->_table}.id, {$this->_table_member}.wx_open_id as open_id");
        $this->db->from( $this->_table );
        $this->db->join($this->_table_member, "{$this->_table_member}.id = {$this->_table}.member_id", 'left');

        $this->db->where($this->_table.'.status',0);
        $this->db->where("UNIX_TIMESTAMP(concat_ws(' ',book_date,book_date_start)) <= ",time()+ 60*60);

        return $this->db->get()->result_array();
    }

    /**
     * 老师提前一小时通知上课
     * @return array
     */
    public function tbook_before()
    {
        $this->db->select("{$this->_table}.id, {$this->_table_teacher}.wx_open_id as open_id");
        $this->db->from( $this->_table );
        $this->db->join($this->_table_teacher, "{$this->_table_teacher}.id = {$this->_table}.teacher_id", 'left');

        $this->db->where($this->_table.'.status',0);
        $this->db->where("UNIX_TIMESTAMP(concat_ws(' ',book_date,book_date_start)) <= ",time()+ 60*60);

        return $this->db->get()->result_array();
    }


    /**
     * 用户下课十分钟后通知点击下课
     * @return array
     */
    public function book_end()
    {
        $this->db->select("{$this->_table}.id, {$this->_table_member}.wx_open_id as open_id");
        $this->db->from( $this->_table );
        $this->db->join($this->_table_member, "{$this->_table_member}.id = {$this->_table}.member_id", 'left');

        $this->db->where_in($this->_table.'.status', array('1,6'));
        $this->db->where("UNIX_TIMESTAMP(concat_ws(' ',book_date,book_date_end)) <= ", time()- 60*10);

        return $this->db->get()->result_array();
    }

    /**
     * 老师下课十分钟后通知点击下课
     * @return array
     */
    public function tbook_end()
    {
        $this->db->select("{$this->_table}.id, {$this->_table_teacher}.wx_open_id as open_id");
        $this->db->from( $this->_table );
        $this->db->join($this->_table_member, "{$this->_table_teacher}.id = {$this->_table}.teacher_id", 'left');

        $this->db->where_in($this->_table.'.status', array('1,5'));
        $this->db->where("UNIX_TIMESTAMP(concat_ws(' ',book_date,book_date_end)) <= ", time()- 60*10);

        return $this->db->get()->result_array();
    }
}

